package com.base.home.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.ToastUtil;
import com.base.home.bean.ConcertSelectEntity;
import com.base.home.view.ConcertView;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2020/2/13.
 */

public class ConcertPresenter extends BasePresenter<ConcertView>{
    public ConcertPresenter(ConcertView view, Activity activity) {
        super(view, activity);
    }

    public void initData(String id){
        HashMap<String, Object> param = new HashMap<>();
        param.put("id",id);
        Disposable disposable = RealNetWorkUtil.get()
                .method("api-train/app/index")
                .params(param)
                .build().enqueue(ConcertSelectEntity.class, new CallBackImpl<ConcertSelectEntity>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        if (!TextUtils.isEmpty(msg)) {
                            if (isContainChinese(msg)){
                                mView.showMes(msg);
                                mView.returnHomeEntityError(msg);
                            }
                        }
                    }
                    @Override
                    public void onNext(ConcertSelectEntity concertSelectEntity) {
                        mView.returnHomeEntity(concertSelectEntity);
                    }
                });
        mCompositeDisposable.add(disposable);

    }


    private  boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
    public void loadMore(int page){
        HashMap<String, Object> param = new HashMap<>();
        param.put("page",page);
        param.put("limit",20);
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-train/app/pullAndLoad")
                .content(param)
                .build().enqueueForArray(ConcertSelectEntity.RecentLearnBean.class, new CallBackImpl<List<ConcertSelectEntity.RecentLearnBean>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        if (!TextUtils.isEmpty(msg)) {
                            mView.showMes(msg);
                        }
                    }
                    @Override
                    public void onNext(List<ConcertSelectEntity.RecentLearnBean> recentLearnBeans) {
                        if (recentLearnBeans!=null&&recentLearnBeans.size()>0){
                            mView.returnHomeEntityMore(recentLearnBeans);
                        }else {
                            mView.returnHomeEntityNoMore();
                        }
                    }
                });
        mCompositeDisposable.add(disposable);

    }

}
